package com.yssoft.controller.core;


import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TArea;
import com.yssoft.service.core.AreaService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;


/**
 * @author 于兴达
 * @time 2018/4/26 下午 03:10
 * @说明  区域管理控制器
 */
@Controller
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;
    
    /**
     * 跳转到区域管理信息页面
     * @return
     */
    @RequestMapping("/areaPage")
    public String areaPage(Model model){
    	model.addAttribute("json",JSONArray.toJSON(areaService.queryAreaTableTree()));
        return "core/area/areaList";
    }
    
    /**
     * 获取区域管理数据
     * @return
     */
    @RequestMapping("/findAreaPage")
    @ResponseBody
    public String findPersonPage(){
        String areaTree = areaService.queryAreaTree();
        return  areaTree;
    }
    
    /**
     * 跳转到编辑区域页面
     * @return
     */
    @RequestMapping("areaEditPage")
    public String areaEditPage(String areaId,Model model){
        if(areaId!=null && areaId.trim().length()>0){
            //修改
            Integer aid = Integer.parseInt(areaId);
            TArea area = areaService.queryRoleById(aid);
            TArea parentArea =  areaService.queryRoleById(area.getT_a_id());
            model.addAttribute("area",area);
            model.addAttribute("parentArea",parentArea);
        }
         return "core/area/areaEdit";
    }
    
    /**
     * 保存区域管理信息
     * @param
     * @return
     */
     @RequestMapping("saveArea")
    public String saveArea(ServletRequest request){
         String  id =  request.getParameter("id");
         String  code =  request.getParameter("code");
         String  name =  request.getParameter("name");
         String  pid =  request.getParameter("pid");


        if(id!=null && !"".equals(id)){
            //修改
            TArea area =  areaService.queryRoleById(Integer.parseInt(id));
            area.setName(name);
            area.setCode(code);
            if(pid!=""){
            	area.setT_a_id(Integer.parseInt(pid));
            }else{
            	area.setT_a_id(1);
            }
            areaService.editAcc(area);
        }else{
            TArea area =  new TArea();
            area.setId(Integer.valueOf(code));
            area.setName(name);
            area.setCode(code);
            if(pid!=null && !"".equals(pid)){
                area.setT_a_id(Integer.parseInt(pid));
            }else{
            	area.setT_a_id(1);
            }
            area.setDelete_flag(0);
            areaService.addAcc(area);
        }
        return "redirect:areaPage";
    }
    
    /**
     * 删除区域管理信息
     * @param areaId
     * @return
     */
    @RequestMapping("/delArea")
    @ResponseBody
    public int delArea(String areaId){
        return areaService.delIndustryCategory(areaId);
    }
    /**
     * 新增、修改区域Tree页
     *@author 王永宁
     * @return
     */
    @RequestMapping("/areaTree")
    public String areaTree(Model model) {
//        List<Map<String, Object>> jsonArray = areaService.queryAreaNewTree();
//        System.out.println(jsonArray.size());
//        model.addAttribute("json", JSONArray.toJSON(jsonArray));
    	String areaTree = areaService.queryAreaTree();
    	model.addAttribute("json", areaTree);
        return "core/area/areaNewTree";
    }
}
